package commands;

import storage.Task;
import storage.UndoRedoDetails;
import logic.TaskDirector;
import logic.UndoRedoManager;

public class Redo {

	UndoRedoManager manager = new UndoRedoManager();
	AvailableCommands command_;	
	
	// create instance of each action
	Add add = new Add();
	Remove remove = new Remove();
	Edit edit = new Edit();
	Complete complete = new Complete();
	Search search = new Search();
	
	public String redoTask() {
		String feedback = "";
		UndoRedoDetails detail_ = new UndoRedoDetails();
		detail_ = manager.getAction("redo");
		
		if(detail_ == null){
			return feedback = "Redo is not available.";
		}else{
			AvailableCommands command_ = AvailableCommands.valueOf(detail_.getCommand().toUpperCase());
		
		switch (command_) {
		case ADD :
			feedback = addRemovedTask(detail_);
			break;
		case EDIT :
			feedback = edit.editTaskByField(detail_.getId(), detail_.getEditingField(), detail_.getInitialValue());
			break;
		case REMOVE :
			feedback = remove.removeTask(detail_.getId());
			break;
		case COMPLETE :
			feedback = edit.editTaskByField(detail_.getId(), "status", detail_.getInitialValue());
			break;
		}
			
	}
			return "Redo is successful! " + feedback;
	}
	
	private String addRemovedTask(UndoRedoDetails detail) {
		Task tempTask = new Task();
		tempTask.setId(detail.getId()); // use old id or it will affect repeated undo and redo actions
		assert tempTask.getId() == detail.getId();
		tempTask.setTitle(detail.getTask().getTitle());
		tempTask.setDeadline(detail.getTask().getDeadline());
		tempTask.setStatus(detail.getTask().getStatus());
		tempTask.setPriority(detail.getTask().getPriority());
				
		Task createdTask = TaskDirector.addTask(tempTask);

		return "Task " + createdTask.getTitle() + " added.";
	}	
}

